//
//  TypeTowController.m
//  Streaming
//
//  Created by 官琛 on 15-6-22.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "TypeTowController.h"

@interface TypeTowController ()

@end

@implementation TypeTowController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blueColor];
    self.navigationController.navigationBar.translucent = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:self.titleLabel];
    self.tableView.tableHeaderView = titleView;
    [_titleLabel release];
    [titleView release];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    
    self.arrayTwo = [NSMutableArray array];
    self.number = 1;
    [self afnGetRequest];
    [self footerRefesh];
    [_tableView autorelease];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-unie642.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
}
- (void)leftBarButtonItemAction
{
    [self.navigationController popViewControllerAnimated:YES];
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInfer = @"TypeTwoCell";
    TypeTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfer];
    if (cell == nil) {
        cell = [[TypeTwoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInfer];
    }
    Cemera *cemera = [self.arrayTwo objectAtIndex:indexPath.row];
    cell.cemTwo = cemera;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       return self.arrayTwo.count;


}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220;
}

//上拉加载
- (void)footerRefesh
{
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        //        NSLog(@"%ld", self.number);
        self.isUpLoading = YES;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.tableView reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    }];
    
    
}


-(void)afnGetRequest
{
    NSLog(@"%ld", self.number);
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListDetails.api?pageIndex=%ld&topListId=%ld", self.number, self.TypeID];
    
    self.number++;
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"movies"];
        for (NSMutableDictionary *dic in array) {
            Cemera *ceme2 = [[Cemera alloc]init];
            ceme2.summary = [dic objectForKey:@"remark"];
            ceme2.posterUrl = [dic objectForKey:@"posterUrl"];
            ceme2.birthDay = [dic objectForKey:@"releaseDate"];
            ceme2.birthLocation = [dic objectForKey:@"releaseLocation"];
            ceme2.nameCn = [dic objectForKey:@"name"];
            ceme2.nameEn = [dic objectForKey:@"nameEn"];
            ceme2.sex = [dic objectForKey:@"director"];
            ceme2.idd = [[dic objectForKey:@"id"]integerValue];
            ceme2.actor = [dic objectForKey:@"actor"];
            ceme2.rankNum = [[dic objectForKey:@"rankNum"]integerValue];
            ceme2.rating = [[dic objectForKey:@"rating"]floatValue];
            [self.arrayTwo addObject:ceme2];
            
            self.titleLabel.text = self.typeName;
            [ceme2 release];
        }
     
        [self.tableView reloadData];
        [self.HUD hide:YES];
        [self.tableView.footer endRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
    }];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    MovieDetailViewController *movieDet = [MovieDetailViewController alloc];
    movieDet.hidesBottomBarWhenPushed = YES;
    Cemera *ceme2 = [self.arrayTwo objectAtIndex:indexPath.row];
    movieDet.movieID = ceme2.idd;
    [self.navigationController pushViewController:movieDet animated:YES];
    
    [movieDet release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
